import time
import os

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options

# from webdriver_helper import get_webdriver
# from webdriver_helper.pom import By

# 配置下载路径和文件名
download_dir = "D:\\py_down"  # 需确保路径存在
target_filename = "renamed_file.ext"

# 最新下载的重命名
def rename_latest_file(download_dir, new_name):
    # 获取目录下所有文件并按修改时间排序
    files = [os.path.join(download_dir, f) for f in os.listdir(download_dir)]
    files = [f for f in files if os.path.isfile(f)]
    files.sort(key=lambda x: os.path.getmtime(x), reverse=True)

    if files:
        latest_file = files[0]
        file_ext = os.path.splitext(latest_file)[1]
        new_path = os.path.join(download_dir, f"{new_name}{file_ext}")
        os.rename(latest_file, new_path)
        return new_path
    return None


# 配置下载参数
options = Options()
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.dir", download_dir)
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")

driver = webdriver.Firefox(options=options)
driver.get("https://testbm.htyunjiaohui.com")

# 页面最大化
# driver.maximize_window()
# # 获取页面大小
# driver.get_window_size()
# # 当前页面url
# driver.current_url

# 元素定位
el1 = driver.find_element(By.XPATH, '/html/body/div/div/div/div[2]/form/div[3]/div/div/input')
print(el1.get_attribute('value'))
# 设置值
el1.clear()
el1.send_keys('lwc')
# 文本值定位 精确定位
# el2 = driver.find_element(By.LINK_TEXT,'欢迎来到云交会管理后台！')
# print(el2.get_attribute('class'))
# 文本值定位 模糊定位
el3 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[2]/form/div[4]/div/div/input')
# 清空内容
el3.clear()
el3.send_keys('aaaaa110')
print(el3.get_attribute('value'))
driver.find_element(By.XPATH, '/html/body/div/div/div/div[2]/form/button').click()
# 强制等待
# time.sleep(2)

# 隐式等待 表示10秒内一直查找元素，找到后直接开始下面逻辑
driver.implicitly_wait(10)
driver.find_element(By.XPATH,
                    '/html/body/div/div/div[1]/div[1]/div[1]/div/div[2]/div[1]/div/div/div/div[6]/div').click()
time.sleep(1)
driver.find_element(By.XPATH, '/html/body/div/div/div[1]/div[1]/div[1]/div/ul/li[15]').click()
time.sleep(1)
# 定义一个数组并循环
my_array = ["1", "2", "3", "4", "5"]

for item in my_array:
    el5 = driver.find_element(By.XPATH, '/html/body/div/div/div[1]/div[2]/div[2]/section/div/form/div[4]/div/div/input')
    el5.clear()
    el5.send_keys(item)
    # 下拉选择
    # driver.find_element(By.CSS_SELECTOR,'div.el-form-item:nth-child(2) > div:nth-child(1) > div:nth-child(1)').click()
    # driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div[1]/ul/li[2]').click()

    # div.el-form-item:nth-child(3) > div:nth-child(1) > div:nth-child(1)
    # 查询点击
    driver.find_element(By.XPATH,'/html/body/div/div/div[1]/div[2]/div[2]/section/div/form/div[6]/div/button').click()

    # 导出点击
    driver.find_element(By.XPATH, '/html/body/div/div/div[1]/div[2]/div[2]/section/div/form/div[7]/div/button').click()
    print(item)
    # 等待文件下载时间，根据文件大小调整
    time.sleep(5)
    rename_latest_file(download_dir, "test" + item)

# 关闭浏览器
driver.close()
input()
